/*
 * Copyright (c) 2017, Li Bo/Alex Lee All rights reserved.
 * 
 * http://www.libo4dream.club/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.hyts.fscan.deal;


import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FilenameFilter;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName: NonRecursiveLoader
 * @Description: 非递归操作算法进行遍历所有文件信息
 * @author: LiBo/Alex Lee
 * @date: 2017年9月30日 下午11:19:54
 * @email: alexlibochn@china-pui.com.cn/alex.link@foxmail.com
 */
public class NonRecursiveLoader {

	/**
	 * @fieldName: path
	 * @fieldType: String
	 * @Description: 路径
	 */
	private String path;

	/**
	 * @fieldName: filter
	 * @fieldType: FilenameFilter
	 * @Description: 文件名称过滤器
	 */
	private FilenameFilter filter;
	
	/**
	 * @fieldName: fileArray
	 * @fieldType: File[]
	 * @Description: 文件数组
	 */
	private List files;
	
	/**
	 * @Title:NonRecursiveLoader
	 * @Description:非递归进行加载器
	 */
	public NonRecursiveLoader(String path,FilenameFilter filter) {
		this.path = path;
		this.filter = filter;
	}
	
	/**
	 * @Title:NonRecursiveLoader
	 * @Description:TODO
	 * @param path
	 */
	public NonRecursiveLoader(String path) {
		super();
		this.path = path;
	}

	/**
	 * @Title: scanFiles
	 * @Description: 加载扫描文件
	 * @return
	 * @return: List<File>
	 */
	@SuppressWarnings("unchecked")
	public void scanFiles(){
		//校验path路径操作
		if(StringUtils.isNotEmpty(path))
		{
			File file = new File(path);
			LinkedList<File> directorys = null;
			//文件是否存在
			if(file.exists())
			{
				files = new LinkedList();
				File [] directoryArray = filter == null ? file.listFiles():file.listFiles(filter);
				directorys = new LinkedList<File>();
				for(File dir:directoryArray){
					if(dir.isDirectory())
					{
						directorys.add(dir);
					}
					else
					{
						files.add(dir);
					}
				}
  			}
			File tempFile;
			while(!directorys.isEmpty())
			{
				tempFile = directorys.removeFirst();
				//是否目录
				if(tempFile.isDirectory())
				{
					File [] directoryArray = filter == null ? tempFile.listFiles():tempFile.listFiles(filter);
					if(directoryArray == null)
					{
						continue;
					}
					for(File tempFileInner:directoryArray)
					{
						if(tempFileInner.isDirectory())
						{
							System.out.println(tempFileInner.getName()+" "+tempFileInner.getAbsolutePath());
							directorys.add(tempFileInner);
						}
						else
						{
							files.add(tempFileInner);
						}
					}
				}
				else
				{
					files.add(tempFile);
				}
			}
		}
	}

	public List getFiles() {
		return files;
	}
	
}
